package com.useful.featurewifi.feature.base.http

import android.annotation.SuppressLint
import com.aa.base.PlayerCheck
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLContext
import javax.net.ssl.X509TrustManager

/**
 * @author Afra55
 * @date 4/16/21
 * A smile is the best business card.
 */
class RetrofitHelper {

    companion object {

        val retrofit: Retrofit by lazy {
            val okHttpClient = OkHttpClient().newBuilder()
            if (PlayerCheck.isDebugMode) {
                okHttpClient.addInterceptor(HttpLoggingInterceptor().apply {
                    this.level = HttpLoggingInterceptor.Level.BODY
                })
            }
            val trustAllCerts: Array<X509TrustManager> = arrayOf(
                object : X509TrustManager {
                    @SuppressLint("TrustAllX509TrustManager")
                    override fun checkClientTrusted(
                        chain: Array<out X509Certificate>?,
                        authType: String?
                    ) {

                    }

                    @SuppressLint("TrustAllX509TrustManager")
                    override fun checkServerTrusted(
                        chain: Array<out X509Certificate>?,
                        authType: String?
                    ) {

                    }

                    override fun getAcceptedIssuers(): Array<X509Certificate> {

                        return emptyArray()
                    }
                }
            )

            val sslContext = SSLContext.getInstance("SSL")
            sslContext.init(null, trustAllCerts, SecureRandom())
            val  sslSocketFactory = sslContext.socketFactory;


            val build = okHttpClient
                .sslSocketFactory(sslSocketFactory, trustAllCerts[0])
                .hostnameVerifier(HostnameVerifier { hostname, session -> true })
                .build()
            Retrofit.Builder()
                .client(build)
                .baseUrl("https://www.baidu.com/")
                .addCallAdapterFactory(ErrorHandlingCallAdapterFactory())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(MoshiConverterFactory.create())
                .build()
        }




    }
}